const gulp = require('gulp')
const rename = require('gulp-rename')
const browserSync = require('browser-sync').create()
const babel = require('gulp-babel')
const sass = require('gulp-sass')
const clean   = require('gulp-clean')

const pathConfig = {
  src: 'src/',
  dist: 'dist/'
}

gulp.task('init', function () {
  gulp.src(pathConfig.src + 'assets/**/**')
    .pipe(gulp.dest(pathConfig.dist + 'assets/'))
  gulp.src(pathConfig.src + 'lib/**/**')
    .pipe(gulp.dest(pathConfig.dist + 'lib/'))
})

gulp.task('js', function () {
  return gulp.src(pathConfig.src + 'module/**/*.js')
    .pipe(babel({
      presets: ['env']
    }))
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest(pathConfig.dist + 'module/'))
    .pipe(browserSync.stream())
})

gulp.task('html', function () {
  return gulp.src(pathConfig.src + 'module/**/*.html')
    .pipe(gulp.dest(pathConfig.dist + 'module/'))
    .pipe(browserSync.stream())
})

gulp.task('sass', function () {
  return gulp.src([pathConfig.src + 'module/**/*.scss'])
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest(pathConfig.dist + 'module/'))
    .pipe(browserSync.stream())
})

gulp.task('browser-sync', ['js', 'sass', 'html', 'init'], function () {
  browserSync.init({
    server:{
        baseDir:'./dist'
    },
    port:2160
  })

  gulp.watch(pathConfig.src + 'module/**/*.html', ['html'])
  gulp.watch(pathConfig.src + 'module/**/*.js', ['js']).on('change', function (event) {
    console.log('File ' + event.path + ' was ' + event.type + ', running tasks...')
  })
  gulp.watch(pathConfig.src + 'module/**/*.scss', ['sass']).on('change', function (event) {
    console.log('File ' + event.path + ' was ' + event.type + ', running tasks...')
  })
})

gulp.task('default', ['browser-sync'])
gulp.task('build', ['js', 'sass', 'html'])